package com.qiangqiang.task.server.service;

import com.qiangqiang.task.server.dto.MyRankingDTO;
import com.qiangqiang.task.server.dto.PostDTO;
import com.qiangqiang.task.server.entity.Post;
import com.qiangqiang.task.server.mapper.PostMapper;
import com.qiangqiang.task.server.mapper.UserMapper;
import com.qiangqiang.task.server.ranking.entity.RankingUser;
import com.qiangqiang.task.server.util.SnowflakeIdWorker;
import javafx.geometry.Pos;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: task
 * @description:
 * @author: Sky.lin
 * @create: 2018-12-20 20:16
 **/
@Service
public class PostService extends BaseService {

    @Autowired
    public PostMapper postMapper;

    @Autowired
    public RankingService rankingService;

    @Autowired
    public UserMapper userMapper;

    SnowflakeIdWorker userIdWorker = new SnowflakeIdWorker(0, 3);


    public boolean addPost(Post post) {
        // 改变用户的成就值,发布一个帖子获得 2 成就值
        rankingService.changeRanking(post.getPublisherId(), 2);
        //
        post.setPostId(userIdWorker.nextId());
        post.setCreateTime(Instant.now().toEpochMilli());
        post.setUpdateTime(Instant.now().toEpochMilli());
        return postMapper.insertPost(post) > 0;
    }

    public List<PostDTO> findAllPost(int page, int pageSize) {
        return postMapper.queryAllPost(page, pageSize).stream().
                map(this::beanToDTO).collect(Collectors.toList());
    }

    public List<PostDTO> findPostByUserId(long userId) {
        return postMapper.queryPostByUserId(userId).stream().
                map(this::beanToDTO).collect(Collectors.toList());
    }
}
